/**
 * @Author Guy Franco
 *
 * 	this is the manage request panel able to search a request, present its details and 
 * update it accordingly
 */

package gui;

import javax.swing.JPanel;

import Entities.RequestEntity;
import common.ChatIF;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JComboBox;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import javax.swing.JTextField;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class ManageRequestPanel extends JPanel implements ChatIF{
	private AcademicFrame frame;
	private ManageRequestPanel manageRequestPanel;
	private JPanel previousPanel;
	private ViewRequestPanel viewRequest;
	private JTextField textField;
	private boolean isSupervisor;
	private String userID;
	private JComboBox comboBox;
	
	
	public ManageRequestPanel(AcademicFrame frame,JPanel previousPanel, boolean isSupervisor, String userID) {
		super();
		this.frame=frame;
		this.userID = userID;
		this.isSupervisor = isSupervisor;
		this.previousPanel=previousPanel;
		this.manageRequestPanel=this;
		frame.setSize(500, 400);
		
		initialize();
		
	}
	
	
private void initialize() {	
		
		setLayout(null);
		
		JLabel lblManageRequests = new JLabel("Manage Requests");
		lblManageRequests.setFont(new Font("Tahoma", Font.BOLD, 18));
		lblManageRequests.setForeground(Color.BLUE);
		lblManageRequests.setBounds(131, 29, 194, 22);
		add(lblManageRequests);
		
		JLabel lblSelectARequest = new JLabel("Request ID:");
		lblSelectARequest.setBounds(48, 75, 214, 14);
		add(lblSelectARequest);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frame.setSize(500, 500);
				frame.setContentPane(previousPanel);
			}
		});
		btnCancel.setBounds(173, 169, 89, 23);
		add(btnCancel);
		
		JButton btnViewEdit = new JButton("View");
		btnViewEdit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(comboBox.getSelectedIndex()==0)
				{
					(new Dialog("Please choose a request to manage")).setVisible(true);
				}
				else
				{
					ArrayList<Object> request =new ArrayList<Object>();
					request.add("trackRequest");
					request.add(comboBox.getSelectedItem().toString());
					AcademicFrame.myclient.sendMessageToServer(request);
				}
			}
		});
		btnViewEdit.setBounds(257, 99, 112, 23);
		add(btnViewEdit);
		
		comboBox = new JComboBox();
		comboBox.setBounds(48, 99, 169, 22);
		add(comboBox);
	}

	@Override
	public void display(Object message) {
		// TODO Auto-generated method stub
		ArrayList<Object> answer=new ArrayList<Object>();
		answer.clear(); // clear the info array list
		answer = (ArrayList<Object>)message;
		
		if (	((String)(answer.get(0))).equalsIgnoreCase("getListOfAllRequests"))
		{
			int numberOfIDs = Integer.parseInt((String)answer.get(1)); // get the amount of items to appear in the combobox
			this.comboBox.addItem("--Choose a request--");
			for (int i=0; i < numberOfIDs ; i++ )
			{
				this.comboBox.addItem((String)answer.get(i+2));  // add requests to the list
			}
		}
		else
		if (	((String)(answer.get(0))).equalsIgnoreCase("requestData")	)
		{
			
				RequestEntity requestDetails = (RequestEntity)(answer.get(1));
				if(requestDetails.getStatusid().equals("999"))
				{
					Integer rid=requestDetails.getRID();
					viewRequest = new ViewRequestPanel(rid.toString(),frame,manageRequestPanel,false, userID);
					frame.setSize(550, 730);
					frame.setContentPane(viewRequest);
				}
				else
				{

					Integer rid=requestDetails.getRID();
					viewRequest = new ViewRequestPanel(rid.toString(),frame,manageRequestPanel,isSupervisor,userID);
					frame.setSize(550, 730);
					frame.setContentPane(viewRequest);
					
				}
			

		}
			
			}
	}


